const router = require("koa-router")()
const sequelize = require("../mysqlconnect.js")
const {QueryTypes} = require("sequelize")
router.prefix("/goods")
router.get("/findGoodsByCategoryId",async (ctx,next)=>{
	const categoryId = ctx.query.categoryId
	// const goodsIdArr = await sequelize.query(`select goodsId from goods_category where categoryId=${categoryId}`,{type:QueryTypes.SELECT})
	
	const res = await sequelize.query(`select * from goods where id in (select goodsId as id from goods_category where categoryId=${categoryId})`,{type:QueryTypes.SELECT})
	ctx.body = {code:200,data:res}
})
router.get("/getSwiper",async (ctx,next)=>{
	const id = ctx.query.id
	const res = await sequelize.query(`select * from swiper where goodsId=${id}`,{type:QueryTypes.SELECT})
	ctx.body = {code:200,data:res}
})
router.get("/getSku",async (ctx,next)=>{
	const id = ctx.query.id
	const res = await sequelize.query(`select * from sku where goodsId=${id}`,{type:QueryTypes.SELECT})
	ctx.body = {code:200,data:res}
})
router.get("/getGoodsInfoRichText",async (ctx,next)=>{
	const id = ctx.query.id
	const res = await sequelize.query(`select info from goods where id=${id}`,{type:QueryTypes.SELECT})
	ctx.body = {code:200,data:res}
})
router.post("/addCart",async (ctx,next)=>{
	const {skuId,goodsNum,checked,createTime,updateTime} = ctx.request.body
	const {id} = ctx.state.user
	const num = await sequelize.query("insert into cart (skuId,goodsNum,checked,createTime,updateTime,userId) values (?,?,?,?,?,?)",
	{replacements:[Number(skuId),Number(goodsNum),Number(checked),createTime,updateTime,id]})
	ctx.body = {code:200,msg:"操作成功"}
})
router.get("/getCart",async (ctx,next)=>{
	const {id} = ctx.state.user
	let res = await sequelize.query("select * from cart where userId = ?",{replacements:[id],type:QueryTypes.SELECT})
	// res.forEach(async (item,i)=>{
	// 	res[i].sku = await sequelize.query("select * from sku where id=?",{replacements:[item.skuId],type:QueryTypes.SELECT})
	// })
	for(let i=0;i<res.length;i++){
		res[i].sku = await sequelize.query("select * from sku where id=?",{replacements:[res[i].skuId],type:QueryTypes.SELECT})
		res[i].goods = await sequelize.query("select * from goods where id=?",
		{replacements:[res[i].sku[0].goodsId],type:QueryTypes.SELECT})
	}
	ctx.body = {
		code:200,
		data:res
	}
})
router.get("/delCart",async (ctx,next)=>{
	const {id} = ctx.state.user
	const {cartId} = ctx.query
	await sequelize.query("delete from cart where id=? and userId=?",{replacements:[cartId,id]})
	ctx.body={code:200,msg:"操作成功"}
})
router.get("/updateCartNumber",async(ctx,next)=>{
	const {id} = ctx.state.user
	const {number,cartId} = ctx.query
	await sequelize.query("update cart set goodsNum=? where id=? and userId=?",{replacements:[number,cartId,id]})
	ctx.body={code:200,msg:"操作成功"}
})
router.get("/updateCartChecked",async(ctx,next)=>{
	const {id} = ctx.state.user
	const {checked,cartId} = ctx.query
	await sequelize.query("update cart set checked=? where id=? and userId=?",{replacements:[checked,cartId,id]})
	ctx.body={code:200,msg:"操作成功"}
})
router.get("/getGoodsByIdAndSkuId",async (ctx,next)=>{
	const {id,skuId} = ctx.query
	let res = await sequelize.query("select * from goods where id=?",{type:QueryTypes.SELECT,replacements:[Number(id)]})
	for(i=0;i<res.length;i++){
		res[i].sku = await sequelize.query("select * from sku where id=?",{type:QueryTypes.SELECT,replacements:[Number(skuId)]})
	}
	let data = [{goods:res[0]}]
	ctx.body = {code:200,data}
	// console.log(res)
})
router.get("/getGoodsByCartIds",async (ctx,next)=>{
	const {cartIds} = ctx.query
	const res = await sequelize.query(`select * from cart where id in (${cartIds})`,{type:QueryTypes.SELECT})
	for(i=0;i<res.length;i++){
		sku = await sequelize.query(`select * from sku where id=${res[i].skuId}`,{type:QueryTypes.SELECT})
		goods = await sequelize.query(`select * from goods where id=${sku[0].goodsId}`,{type:QueryTypes.SELECT})
		res[i].goods = goods[0]
		res[i].goods.sku = sku
	}
	ctx.body = {code:200,data:res}
})
router.post("/createOrder",async(ctx,next)=>{
	const {id} = ctx.state.user
	const {cart_ids,goods_id,goodsNum,goods_sku_id,order_coupon_id,order_integral,order_use_integral,addr_id,createTime,updateTime} = ctx.request.body
	const res = await sequelize.query("insert into `resource-uniapp-all`.`goods_order` (cart_ids,goods_id,goodsNum,goods_sku_id,order_coupon_id,order_integral,order_use_integral,addr_id,createTime,updateTime,userId,state) values (?,?,?,?,?,?,?,?,?,?,?,?)",
	{replacements:[cart_ids,goods_id,goodsNum,goods_sku_id,Number(order_coupon_id),Number(order_integral),Number(order_use_integral),Number(addr_id),createTime,updateTime,id,"待核实"]})
	ctx.body = {code:200,msg:"操作成功"}
})
router.get("/queryOrders",async(ctx,next)=>{
	const {id} = ctx.state.user
	const res = await sequelize.query("select * from goods_order where userId=?",{type:QueryTypes.SELECT,replacements:[id]})
	// console.log(res)
	let data = []
	for(i=0;i<res.length;i++){
		// console.log(i)
		// console.log(data,res[i].state)
		const d = {}
		d.order = {}
		d.order.state = res[i].state
		d.order.id = res[i].id
		d.order.order_actural_paid = 10000
		d.order_detail = []
		if(res[i].cart_ids==""){
			const a = await sequelize.query("select * from goods where id=?",{type:QueryTypes.SELECT,replacements:[res[i].goods_id]})
			const b = await sequelize.query("select * from sku where id=?",{type:QueryTypes.SELECT,replacements:[res[i].goods_sku_id]})
			console.log("i",i,res[i].cart_ids)
			// console.log(data)
			let dor = {}
			dor.goods_img = a[0].img
			dor.goods_name = a[0].name+b[0].name
			dor.goods_num = res[i].goodsNum
			dor.goods_price = b[0].price 
			// console.log(data)
			d.order_detail.push(dor)
			data.push(d)
		}else{
			console.log("cart")
			// data[i] = "cccc"
			let cs = res[i].cart_ids.split(",")
			for(let ii=0;ii<cs.length;ii++){
				const c = await sequelize.query("select * from cart where id=?",{type:QueryTypes.SELECT,replacements:[Number(cs[ii])]})
				// data[i] = c
				const b = await sequelize.query("select * from sku where id=?",{type:QueryTypes.SELECT,replacements:[c[0].skuId]})
				const a = await sequelize.query("select * from goods where id=?",{type:QueryTypes.SELECT,replacements:[b[0].goodsId]})
				// d.order_detail = [{}]
				// console.log(data)
				let dor = {}
				dor.goods_img = a[0].img
				dor.goods_name = a[0].name+b[0].name
				dor.goods_price = b[0].price
				let goodsNum = res[i].goodsNum.split(",")[ii]
				dor.goods_num = goodsNum
				d.order_detail.push(dor)
			}
			data.push(d)
		}
		
	}
	ctx.body={code:200,data}
})
router.get("/queryOrderById",async (ctx,next)=>{
	const {id} = ctx.state.user
	const {orderId} = ctx.query
	const res = await sequelize.query("select * from goods_order where userId=? and id=?",{type:QueryTypes.SELECT,replacements:[id,orderId]})
	// console.log(res)
	let data = [{}]
	for(i=0;i<res.length;i++){
		// console.log(i)
		// console.log(data,res[i].state)
		data[i].order = {}
		data[i].order.state = res[i].state
		data[i].order.id = res[i].id
		data[i].order.createTime = res[i].createTime
		data[i].order.order_actural_paid = 10000
		
		const a = await sequelize.query("select * from goods where id=?",{type:QueryTypes.SELECT,replacements:[res[i].goods_id]})
		const b = await sequelize.query("select * from sku where id=?",{type:QueryTypes.SELECT,replacements:[res[i].goods_sku_id]})
		data[i].order_detail = [{}]
		// console.log(data)
		data[i].order_detail[0].goods_img = a[0].img
		data[i].order_detail[0].goods_name = a[0].name+b[0].name
		data[i].order_detail[0].goods_num = res[i].goodsNum
		data[i].order_detail[0].goods_price = b[0].price
		// console.log(data)
	}
	ctx.body={code:200,data}
})
module.exports = router
// INSERT INTO `resource-uniapp-all`.`goods_order` (`cart_ids`, `goods_id`, `goodsNum`, `goods_sku_id`, `order_coupon_id`, `order_integral`, `order_use_integral`, `addr_id`, `createTime`, `updateTime`) VALUES ('\'\'', 1, 1, 1, 1, 1, 1, 1, '2024-12-17 23:46:04', '2024-12-17 23:46:07')